package com.summer.data.jql.builder;

import com.summer.data.Jsql;
import com.summer.data.dialect.Dialect;
import com.summer.data.exception.JqlGrammarException;
import com.summer.data.jql.Context;
import com.summer.data.jql.JsqlAble;
import com.summer.data.jql.behavior.FilterAble;
import com.summer.data.jql.behavior.TableAble;
import com.summer.data.jql.builder.from.FromNode;
import com.summer.data.jql.builder.from.JOIN_TYPE;
import com.summer.data.jql.builder.from.JoinNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 345480567@qq.com
 */

public class FromBuilder implements JsqlAble {

    List<JoinNode> nodeList = new ArrayList<>();

    Context ctx;

    public FromBuilder(Context ctx) {
        this.ctx = ctx;
    }

    public void from(TableAble tableAble) {
        nodeList.add(new FromNode(ctx, tableAble));
    }

    public void join(TableAble tableAble) {
        nodeList.add(new JoinNode(ctx, JOIN_TYPE.JOIN, tableAble));
    }

    public void leftJoin(TableAble tableAble) {
        nodeList.add(new JoinNode(ctx, JOIN_TYPE.LEFT_JOIN, tableAble));
    }

    public void rightJoin(TableAble tableAble) {
        nodeList.add(new JoinNode(ctx, JOIN_TYPE.RIGHT_JOIN, tableAble));
    }

    public void innerJoin(TableAble tableAble) {
        nodeList.add(new JoinNode(ctx, JOIN_TYPE.INNER_JOIN, tableAble));
    }

    public void outerJoin(TableAble tableAble) {
        nodeList.add(new JoinNode(ctx, JOIN_TYPE.OUTER_JOIN, tableAble));
    }

    public void on(FilterAble... exps) {
        for (FilterAble exp : exps) {
            and(exp);
        }
    }

    public void and(FilterAble exp) {
        if (nodeList.isEmpty()) {
            throw new JqlGrammarException("JOIN 列表为空，不能添加条件: ");
        }
        JoinNode lastJoin = nodeList.get(nodeList.size()-1);
        lastJoin.and(exp);
    }

    public void or(FilterAble exp) {
        if (nodeList.isEmpty()) {
            throw new JqlGrammarException("JOIN 列表为空，不能添加条件:");
        }
        JoinNode lastJoin = nodeList.get(nodeList.size()-1);
        lastJoin.or(exp);
    }

    @Override
    public Jsql buildJsql(Dialect dialect) {
        Jsql jsql = new Jsql();

        for (JoinNode node : nodeList) {
            jsql.append(node.buildJsql(dialect));
            jsql.append(System.lineSeparator());
        }

        return jsql;
    }

    public void init() {
        if (nodeList == null || nodeList.size() == 0) {
            throw new JqlGrammarException("没有设定查询的表，无法构建 Jql。");
        }

//        Map<String, Integer> numMap = new HashMap<String, Integer>(nodeList.size()+1);

        // 再处理 join
//        for(JoinNode node : nodeList) {
//            TableAble tableAble = node.tableAble;
//
//            // 只有未设定的才自动处理
//            if (tableAble.getAlias() == null) {
//                if (tableAble instanceof BaseTable) {
//                    BaseTable table = (BaseTable)tableAble;
//                    String tableName = table.getTableName();
//                    if (numMap.containsKey(tableName)) {
//                        int num = numMap.get(tableName)+1;
//                        numMap.put(tableName, num);
//                        tableAble.alias(tableName+"_"+num);
//                    } else {
//                        numMap.put(tableName, 0);
//                    }
//                }
//            }
//        }

    }

}
